package com.is.control;

import java.sql.ResultSet;

import com.is.dbconn.DBConnect;
import com.is.model.ParamCourseScoreGS;
/**
 * 处理每门课程的成绩计算公式
 * @author Administrator
 *
 */
public class ParamCourseScoreGSControl {
	
	public static ParamCourseScoreGS getGSByCourseClassId(int courseClassId) {
		DBConnect dbConnect = new DBConnect();
		String sql = "select * from param_coursescore_gs where courseClassId = '"+courseClassId+"'";
		dbConnect.prepareStatement(sql);
		ResultSet rs = null;
		ParamCourseScoreGS gs = null;;
		try {
			rs = dbConnect.executeQuery();
			if(rs.next()){
				gs = new ParamCourseScoreGS();
				gs.setCourseClassId(courseClassId);
				gs.setId(rs.getInt("id"));
				gs.setDailyScore_per(rs.getString("dailyScore_per"));
				gs.setMiderScore_per(rs.getString("midtermScore_per"));
				gs.setFinalScore_per(rs.getString("finalScore_per"));
				gs.setAttendance_per(rs.getString("attendance_per"));
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("isInserted ERROR");
		}finally{
			dbConnect.close();
		}
		return gs;
	}
	
	public static boolean insertGS(ParamCourseScoreGS gs) {
		DBConnect dbConnect = new DBConnect();
		String sql = "insert into param_coursescore_gs(courseClassId,attendance_per,dailyScore_per,midtermScore_per,finalScore_per) values(?,?,?,?,?)";
		boolean flag = false;
		dbConnect.prepareStatement(sql);
		try {
			int index=1;
			dbConnect.setInt(index++, gs.getCourseClassId());
			dbConnect.setString(index++, gs.getAttendance_per());
			dbConnect.setString(index++, gs.getDailyScore_per());
			dbConnect.setString(index++, gs.getMiderScore_per());
			dbConnect.setString(index++, gs.getFinalScore_per());
			flag = dbConnect.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("insertGS　ERROR");
		}finally{
			dbConnect.close();
		}
		return flag;
	}
	

	public static boolean updateGS(ParamCourseScoreGS gs,int courseClassId) {
		DBConnect dbConnect = new DBConnect();
		String sql = "update param_coursescore_gs set courseClassId=?,attendance_per=?,dailyScore_per=?,midtermScore_per=?,finalScore_per=? where courseClassId=?";
		boolean flag = false;
		dbConnect.prepareStatement(sql);
		try {
			int index=1;
			dbConnect.setInt(index++, gs.getCourseClassId());
			dbConnect.setString(index++, gs.getAttendance_per());
			dbConnect.setString(index++, gs.getDailyScore_per());
			dbConnect.setString(index++, gs.getMiderScore_per());
			dbConnect.setString(index++, gs.getFinalScore_per());
			dbConnect.setInt(index++, gs.getCourseClassId());
			flag = dbConnect.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("updateGS　ERROR");
		}finally{
			dbConnect.close();
		}
		return flag;
	}
	
	public static boolean isInserted(int courseClassId) {
		DBConnect dbConnect = new DBConnect();
		String sql = "select id from param_coursesocre_gs where courseClassId = '"+courseClassId+"'";
		dbConnect.prepareStatement(sql);
		ResultSet rs = null;
		boolean flag = false;
		try {
			rs = dbConnect.executeQuery();
			if(rs.next()){
				flag = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("isInserted ERROR");
		}finally{
			dbConnect.close();
		}
		return flag;
	}
	
	public static void main(String args[])  {
		ParamCourseScoreGS gs = getGSByCourseClassId(28);
		System.out.println(gs.getAttendance_per());
				
	}

}
